<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-06-03 18:30:49
 * @LastEditors: Sam
 * @LastEditTime: 2021-12-09 22:14:04
 */


namespace app\__MODULE__\controller;
use app\__MODULE__\model\ Notice;
use app\__MODULE__\model\ NoticeType as NoticeTypeModel ;

/**
 * 奖品类型
 * @author Sam
 * date 2021-11-03
 */
class NoticeType extends Base{
    
    public function index(){
        if(IS_POST) {
            $search  = request()->param();
            $where = [];
            $db = NoticeTypeModel::where($where)->order('sort desc');
            $list = $db->limit(paginator())->select();
            admin_response(0, 'success', $list, $db->count());
        }
        
        return $this->render_table([
            'page'   => true,
            'cols'   => [
                ['type'=>'checkbox'],
                ['field'=>'id', 'title'=>'编号' ,'align'=>'center'],
                ['field'=>'title', 'title'=>'分类名称','align'=>'center'],
              
            ],
           'toolbar' => [
                table_button('__MODULE__/NoticeType/add','添加','frame','',['450px','350px']),
            ],
        
            'toolbar_row' => [
                table_button('__MODULE__/NoticeType/edit', '编辑','frame','',['450px','350px']),
                table_button('__MODULE__/NoticeType/del', '删除', 'confirm'),
            ]
            
            ],false);
        return $this->fetch();
    }

    /**
     * 添加
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function add(){
        if(request()->isPost()) {
           $data = request()->post();
           if(NoticeTypeModel::create($data)) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
           return $this->render_form([
               ['type'=>'text', 'name'=>'title', 'title'=>'分类名称', 'lay-verify'=>'required'],
               ['type'=>'text', 'text_type'=>'number','name'=>'sort', 'title'=>'排序'],


           ]);
        }
    }

    /**
     * 编辑
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function edit(){
        $id = param_check('id');
        if(request()->isPost()) {
           $data = request()->post();
           if(NoticeTypeModel::update($data,['id'=>$id])) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
            $data = NoticeTypeModel::get($id);
           return $this->render_form([
            ['type'=>'text', 'name'=>'title', 'title'=>'分类名称', 'lay-verify'=>'required'],
            ['type'=>'text', 'text_type'=>'number','name'=>'sort', 'title'=>'排序'],
            ],$data);
        }
    }


        /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function del(){
        $id = param_check('id');
        if(!$medal = NoticeTypeModel::get($id)) json_response(0,'不存在');
        if($medal->delete()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }
}